/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package menu.service;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Response;
import menu.Drzava;

/**
 *
 * @author abosancic
 */
@Stateless
@Path("menu.drzava")
public class DrzavaFacadeREST extends AbstractFacade<Drzava> {
    @PersistenceContext(unitName = "WebApplication3PU")
    private EntityManager em;

    public DrzavaFacadeREST() {
        super(Drzava.class);
    }

    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Drzava entity) {
        super.create(entity);
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Drzava entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Drzava find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Produces({"application/xml", "application/json"})
    public Response findAll1() {
        
        CacheControl cc = new CacheControl();
        cc.setMaxAge(86400);
        cc.setPrivate(true);
//        cc.
        
        Response.ResponseBuilder builder = Response.ok(super.findAll());
        builder.header("param1", "value1");
        builder.header("param2", "value2");
        builder.header("Access-Control-Allow-Origin", "*");
        builder.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
        builder.header("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
        builder.cacheControl(cc);
        return builder.build();
        
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Drzava> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public Response countREST() {
        
        
        //http://localhost:8080/WebApplication3/test-resbeans.html
        
        CacheControl cc = new CacheControl();
        cc.setMaxAge(86400);
        cc.setPrivate(true);
//        cc.
        
        Response.ResponseBuilder builder = Response.ok(String.valueOf(super.count()));
        builder.header("param1", "value1");
        builder.header("param2", "value2");
        builder.header("Access-Control-Allow-Origin", "*");
        builder.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
        builder.header("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
        builder.cacheControl(cc);
        return builder.build();
        
        
        //return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
